package com.atguigu.gmall.gateway;

import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;

/**
 * 测试响应式编程代码
 */
public class ReactorMainTestApplication {

//    public static void main(String[] args) {
//        List<String> strings = Arrays.asList("fdsfds", "", "1", "dhjkh", "2");
//
//        Stream<String> stream = strings.stream();
//
//        List<String> collect = stream
//                .map(ele -> "map_" + ele)
//                .filter(ele -> ele.length() > 6)
//                .collect(Collectors.toList());
//
//    }

    /**
     * Publisher：数据发布者
     * Subscriber：数据接受者
     * @param args
     */
    public void test1(String[] args) {


        /**
         * 准备一个源源不断的数据流
         */

//        //纯异步的回调
//        Flux<Integer> range = Flux.range(1,100);
//
//
//
//        //背压
//        range.subscribe((ele)->{
//            System.out.println("ele = " + ele);
//        });

        List<String> a = new ArrayList<>();
        CompletableFuture.runAsync(()->{

            while (true){
                a.add(UUID.randomUUID().toString());
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                }
            }

        });

        Flux<String> flux = Flux.from(Mono.just("ddd"));

        flux.subscribe((ele)->{
            System.out.println("ele = " + ele);
        });

    }


    public static void main(String[] args) throws InterruptedException {
//        ScheduledExecutorService executors = Executors.newScheduledThreadPool(4);
//        executors.scheduleAtFixedRate(()->{
//            System.out.println("11");
//        },10,5, TimeUnit.SECONDS); //定时任务，线程启动10s后，每5s运行一次


        //Quartz   @Scheduled(cron = "")
        //项目一启动，按照指定时间运行的。

        //分布式定时任务。Elastic-Job

//       Thread thread = new Thread(()->{
//           while (true){
//               try {
//                   Thread.sleep(1000);
//               } catch (InterruptedException e) {
//                   e.printStackTrace();
//               }
//               System.out.println("dddddd");
//           }
//        });
//       thread.setDaemon(true);//设置为守护线程，不可以提交给线程池的
//       thread.start();
//
//        System.out.println("主线程等待...");
//       Thread.sleep(10000);
//        System.out.println("主线程结束...");


    }
}
